/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.store.StoreManager;
/*     */ import com.zimbra.cs.store.file.BlobDeduper;
/*     */ import com.zimbra.cs.store.file.FileBlobStore;
/*     */ import com.zimbra.cs.volume.Volume;
/*     */ import com.zimbra.cs.volume.VolumeManager;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.admin.message.DedupeBlobsRequest;
/*     */ import com.zimbra.soap.admin.message.DedupeBlobsRequest.DedupAction;
/*     */ import com.zimbra.soap.admin.message.DedupeBlobsResponse;
/*     */ import com.zimbra.soap.admin.message.DedupeBlobsResponse.DedupStatus;
/*     */ import com.zimbra.soap.admin.type.IntIdAttr;
/*     */ import com.zimbra.soap.admin.type.VolumeIdAndProgress;
/*     */ import java.io.IOException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class DedupeBlobs
/*     */   extends AdminDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  46 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  47 */     checkRight(zsc, context, null, AdminRight.PR_SYSTEM_ADMIN_ONLY);
/*  48 */     StoreManager sm = StoreManager.getInstance();
/*  49 */     if (!(sm instanceof FileBlobStore)) {
/*  50 */       throw ServiceException.INVALID_REQUEST(sm.getClass().getName() + " is not supported", null);
/*     */     }
/*     */     
/*  53 */     DedupeBlobsRequest req = (DedupeBlobsRequest)JaxbUtil.elementToJaxb(request);
/*  54 */     BlobDeduper deduper = BlobDeduper.getInstance();
/*  55 */     DedupeBlobsResponse resp = new DedupeBlobsResponse();
/*     */     
/*  57 */     List<IntIdAttr> volumeList = req.getVolumes();
/*  58 */     List<Short> volumeIds = new ArrayList();
/*  59 */     if ((req.getAction() == DedupeBlobsRequest.DedupAction.start) || (req.getAction() == DedupeBlobsRequest.DedupAction.reset))
/*     */     {
/*  61 */       if (volumeList.isEmpty())
/*     */       {
/*  63 */         for (Volume vol : VolumeManager.getInstance().getAllVolumes()) {
/*  64 */           switch (vol.getType()) {
/*     */           case 1: 
/*     */           case 2: 
/*  67 */             volumeIds.add(Short.valueOf(vol.getId()));
/*     */           
/*     */           }
/*     */           
/*     */         }
/*     */       } else {
/*  73 */         for (IntIdAttr attr : volumeList) {
/*  74 */           short volumeId = (short)attr.getId();
/*  75 */           Volume vol = VolumeManager.getInstance().getVolume(volumeId);
/*  76 */           if (vol.getType() == 10) {
/*  77 */             throw ServiceException.INVALID_REQUEST("Index volume " + volumeId + " is not supported", null);
/*     */           }
/*  79 */           volumeIds.add(Short.valueOf(volumeId));
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*  84 */     if (req.getAction() == DedupeBlobsRequest.DedupAction.start) {
/*     */       try {
/*  86 */         deduper.process(volumeIds);
/*     */       } catch (IOException e) {
/*  88 */         throw ServiceException.FAILURE("error while deduping", e);
/*     */       }
/*  90 */     } else if (req.getAction() == DedupeBlobsRequest.DedupAction.stop) {
/*  91 */       deduper.stopProcessing();
/*  92 */     } else if (req.getAction() == DedupeBlobsRequest.DedupAction.reset) {
/*  93 */       if (volumeList.isEmpty()) {
/*  94 */         deduper.resetVolumeBlobs(new ArrayList());
/*     */       } else {
/*  96 */         deduper.resetVolumeBlobs(volumeIds);
/*     */       }
/*     */     }
/*     */     
/* 100 */     boolean isRunning = deduper.isRunning();
/* 101 */     if (isRunning) {
/* 102 */       resp.setStatus(DedupeBlobsResponse.DedupStatus.running);
/*     */     } else {
/* 104 */       resp.setStatus(DedupeBlobsResponse.DedupStatus.stopped);
/*     */     }
/* 106 */     Map<Short, String> volumeBlobsProgress = deduper.getVolumeBlobsProgress();
/* 107 */     VolumeIdAndProgress[] blobProgress = new VolumeIdAndProgress[volumeBlobsProgress.size()];
/* 108 */     int i = 0;
/* 109 */     for (Map.Entry<Short, String> entry : volumeBlobsProgress.entrySet()) {
/* 110 */       blobProgress[(i++)] = new VolumeIdAndProgress(String.valueOf(entry.getKey()), (String)entry.getValue());
/*     */     }
/* 112 */     resp.setVolumeBlobsProgress(blobProgress);
/* 113 */     Map<Short, String> blobDigestsProgress = deduper.getBlobDigestsProgress();
/* 114 */     VolumeIdAndProgress[] digestProgress = new VolumeIdAndProgress[blobDigestsProgress.size()];
/* 115 */     i = 0;
/* 116 */     for (Map.Entry<Short, String> entry : blobDigestsProgress.entrySet()) {
/* 117 */       digestProgress[(i++)] = new VolumeIdAndProgress(String.valueOf(entry.getKey()), (String)entry.getValue());
/*     */     }
/* 119 */     resp.setBlobDigestsProgress(digestProgress);
/* 120 */     Pair<Integer, Long> pair = deduper.getCountAndSize();
/* 121 */     resp.setTotalCount(((Integer)pair.getFirst()).intValue());
/* 122 */     resp.setTotalSize(((Long)pair.getSecond()).longValue());
/* 123 */     return JaxbUtil.jaxbToElement(resp);
/*     */   }
/*     */   
/*     */   public void docRights(List<AdminRight> relatedRights, List<String> notes)
/*     */   {
/* 128 */     notes.add("Only system admins are allowed.");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/DedupeBlobs.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */